<template>
  <!-- 歌手详情页 -->
  <div class="music-list">
    <div class="list-header">
      <div class="back" @click="$router.go(-1)"><i class="icon-back"></i></div>
      <h1 class="title">{{ title }}</h1>
    </div>
    <div class="bg-image" :style="bgImageStyle" ref="bgImage">
      <div class="play-btn-wrapper" :style="playBtnStyle">
        <div v-show="songs.length > 0" class="play-btn" @click="random">
          <i class="icon-play"></i>
          <span class="text">随机播放全部</span>
        </div>
      </div>
      <!--遮罩层-->
      <div class="filter"></div>
    </div>
    <!--probeType>0才触发滚动事件-->
    <Scroll
      class="list"
      :style="scrollStyle"
      v-loading="loading"
      v-no-result="noResult"
      :probe-type="3"
      @scroll="onScroll"
    >
      <div class="song-list-wrapper">
        <song-list :songs="songs" @select="selectItem"></song-list>
      </div>
    </Scroll>
  </div>
</template>

<script>
import Scroll from '@/components/base/scroll/scroll.vue'
import SongList from '@/components/base/song-list/song-list.vue'
import { mapActions, mapState } from 'vuex'

const RESERVED_HEIGHT = 40
export default {
  name: 'music-list',
  components: {
    Scroll,
    SongList
  },
  props: {
    songs: {
      type: Array,
      default () {
        return []
      }
    },
    loading: Boolean,
    title: String,
    // 背景图片
    pic: String
  },
  data () {
    return {
      imageHeight: 0, // 动态获取图片高度
      scrollY: 0,
      maxTranslateY: 0 // 可滚动最大高度
    }
  },
  mounted () {
    this.imageHeight = this.$refs.bgImage.clientHeight
    this.maxTranslateY = this.imageHeight - RESERVED_HEIGHT
  },
  computed: {
    ...mapState(['playlist']),
    /**
     * 需求：
     * 1、向上滚动：滚动到标题时，保留40px背景图片
     * 2、向下拉：背景图片随滚动距离放大
     */
    bgImageStyle () {
      const scrollY = this.scrollY
      let zIndex = 0
      let paddingTop = '70%'
      let height = 0
      let scale = 1

      if (scrollY > this.maxTranslateY) {
        zIndex = 10
        paddingTop = 0
        // 标题占位，防止歌曲列表滚动到页面顶部
        height = `${RESERVED_HEIGHT}px`
      }
      if (scrollY < 0) {
        scale = scale + Math.abs(scrollY / this.imageHeight)
      }
      return {
        zIndex,
        paddingTop,
        height,
        backgroundImage: `url(${this.pic})`,
        transform: `scale(${scale})`
      }
    },
    scrollStyle () {
      const bottom = this.playlist ? '60px' : 0
      return {
        top: `${this.imageHeight}px`,
        bottom
      }
    },
    noResult () {
      // loading结束且歌曲为空
      return !this.loading && !this.songs.length
    },
    playBtnStyle () {
      let display = ''
      if (this.scrollY >= this.maxTranslateY) {
        display = 'none'
      }
      return { display }
    }
  },
  methods: {
    ...mapActions(['selectPlay', 'randomPlay']),
    onScroll (position) {
      this.scrollY = -position.y
    },
    selectItem ({ song, index }) {
      this.selectPlay({
        list: this.songs,
        index
      })
    },
    random () {
      this.randomPlay(this.songs)
    }
  }
}
</script>

<style lang="scss" scoped>
.music-list {
  height: 100%;
  .list-header {
    z-index: 20;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 40px;
    display: flex;
    justify-content: center;
    align-items: center;
    .back {
      position: absolute;
      left: 4px;
      top: 50%;
      transform: translateY(-50%);
      .icon-back {
        padding: 10px;
        font-size: $font-size-large-x;
        color: $color-theme;
      }
    }
  }
  .bg-image {
    position: relative;
    width: 100%;
    // !! 内边距的高度是元素宽度的 70%。
    padding-top: 70%;
    background-size: cover;
    .play-btn-wrapper{
      z-index: 10;
      position: absolute;
      left: 50%;
      bottom: 20px;
      transform: translateX(-50%);
      .play-btn{
        display: flex;
        align-items: center;
        padding: 8px 18px;
        border-radius: 100px;
        border: 1px solid $color-theme;
        color: $color-theme;
        font-size: $font-size-small;
      }
      .icon-play{
        margin-right: 8px;
        font-size: $font-size-medium-x;
      }
    }
    .filter {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: rgba(7, 17, 27, 0.4);
    }
  }
  .list {
    position: absolute;
    bottom: 0;
    // loading可居中显示
    width: 100%;
    // 滚动时隐藏多余列表
    //overflow: hidden;
    .song-list-wrapper {
      padding: 20px 30px;
      background: $color-background;
    }
  }
}
</style>
